package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.Article;
import com.newway.back.entity.Collections;
import com.newway.back.mapper.ArticleMapper;
import com.newway.back.mapper.CollectionsMapper;
import com.newway.common.context.UserInfo;
import com.newway.common.context.UserInfoContext;
import com.newway.common.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/article")
public class ArticleController {

    @Resource
    ArticleMapper articleMapper;

    @Resource
    CollectionsMapper collectionsMapper;

    @GetMapping("/list")
    public Result getList(Article article){
        Page<Article> articlePage = new Page<>(article.getPageNum(),article.getPageSize());
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(article.getTitle())){
            articleQueryWrapper.like("title",article.getTitle());
        }
        if(StringUtils.isNotBlank(article.getArticleType())){
            articleQueryWrapper.eq("article_type",article.getArticleType());
        }
        UserInfo userInfo = UserInfoContext.getUserInfo();
        IPage<Article> articleIPage = articleMapper.selectPage(articlePage, articleQueryWrapper);
        return Result.success(articleIPage);
    }

    @GetMapping("/listMy")
    public Result listMy(Article article){
        Page<Article> articlePage = new Page<>(article.getPageNum(),article.getPageSize());
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(article.getTitle())){
            articleQueryWrapper.like("title",article.getTitle());
        }
        if(StringUtils.isNotBlank(article.getArticleType())){
            articleQueryWrapper.eq("article_type",article.getArticleType());
        }
        UserInfo userInfo = UserInfoContext.getUserInfo();
        articleQueryWrapper.eq("user_id",userInfo.getId());
        IPage<Article> articleIPage = articleMapper.selectPage(articlePage, articleQueryWrapper);
        return Result.success(articleIPage);
    }

    @GetMapping("/collectList")
    public Result collectList(Article article){
        Page<Article> articlePage = new Page<>(article.getPageNum(),article.getPageSize());
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(article.getTitle())){
            articleQueryWrapper.like("title",article.getTitle());
        }
        if(StringUtils.isNotBlank(article.getArticleType())){
            articleQueryWrapper.eq("article_type",article.getArticleType());
        }

        QueryWrapper<Collections> collectionsQueryWrapper = new QueryWrapper<>();
        collectionsQueryWrapper.eq("user_id", UserInfoContext.getUserInfo().getId());
        collectionsQueryWrapper.eq("collection","1");
        List<Collections> collections = collectionsMapper.selectList(collectionsQueryWrapper);
        List<Integer> collect = collections.stream().map(Collections::getArticleId).collect(Collectors.toList());
        if(collect.isEmpty()){
            articleQueryWrapper.eq("id","-1");
        }else{
            articleQueryWrapper.in("id",collect);
        }

        IPage<Article> articleIPage = articleMapper.selectPage(articlePage, articleQueryWrapper);
        return Result.success(articleIPage);
    }

    @GetMapping("/details/{id}")
    public Result details(@PathVariable("id") Integer id){
        Article article = articleMapper.selectById(id);
        return Result.success(article);
    }

    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Article article){
        if(article.getId() == null){
            article.setCreateTime(LocalDateTime.now());
            article.setUserId(UserInfoContext.getUserInfo().getId());
            articleMapper.insert(article);
        }else{
            article.setUpdateTime(LocalDateTime.now());
            articleMapper.updateById(article);
        }
        return Result.success();
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        articleMapper.deleteById(id);
        return Result.success();
    }
}
